package Shop;

import Shop.damain.Goods;
import Shop.damain.Orders;
import Shop.damain.Users;
import Shop.mappers.GoodsMapper;
import Shop.mappers.UsersMapper;
import Shop.service.GoodService;
import Shop.service.UserService;
import Shop.tools.MybatisUtil;
import Shop.tools.OrdersTools;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Scanner;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        int j=0;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        Goods goods = new Goods(1,"牙刷",12,"生活用品",3);
        System.out.print("欢迎使用电子购物平台\n");
        System.out.print("1.注册\n");
        System.out.print("2.登陆\n");
        System.out.print("3.加入购物车\n");
        System.out.print("4.收藏\n");
        System.out.print("5.支付订单\n");
        System.out.print("6.用实体类的方式\n");
        System.out.print("7.退出\n");
        System.out.print("请输入您需要的业务:\n");
        Scanner scanner = new Scanner(System.in);
        //String msg = scanner.nextLine();
        while (j!=7) {
            String msg = scanner.nextLine();
            Users users = new Users();
            UserService userService = new UserService();
            GoodService goodService = new GoodService();
            switch (Integer.parseInt(msg)) {
                case 1:
                    j = 1;
                    System.out.print("请输入要注册的用户名:");
                    String name = scanner.nextLine();
                    System.out.print("请输入要注册的密码:");
                    String pwd = scanner.nextLine();
                    /*Boolean i = userService.register(name, pwd);
                    if (i)
                        System.out.print("注册成功");
                    else
                        System.out.print("注册失败，请重试");*/

                    Users user = new Users(name,pwd,0,"11",1,"11");
                    usersMapper.addUsers(user);
                    //非查询查询操作必须手动提交
                    sqlSession.commit();
                    MybatisUtil.closeSqlSession();
                    break;
                case 2:
                    j = 2;
                    System.out.print("请输入要登陆的用户名:\n");
                    String name1 = scanner.nextLine();
                    System.out.print("请输入要登陆的密码:\n");
                    String pwd1 = scanner.nextLine();
                    Users user2=new Users(name1,pwd1,0,"11",1,"11");
                    Users user1 = usersMapper.login(user2);
                  /*  users = userService.login(name1,pwd1);*/
                    if (user1 == null)
                        System.out.print("登陆失败");
                    else
                        System.out.print("登陆成功"+user1.getUsername());

                    break;
                case 3:
                    j = 3;
                   /* List<Goods> goodsList = goodService.QueryGoods();*/
                    List<Goods> goodsList = goodsMapper.queryGoods();
                    int k = goodsMapper.queryGoodCounts();
                    System.out.print("总共"+k+"行！");
                    for (int i1 = 0;i1<goodsList.size();i1++)
                        System.out.print("id:"+goodsList.get(i1).getGpid()+" name:"+goodsList.get(i1).getGname()+" "+
                                "price:"+goodsList.get(i1).getGprice()+"\n");
                    System.out.print("请输入选择的商品id");
                    int id = Integer.parseInt(scanner.nextLine());
                    int uid = 6;
                    int oid1= Integer.parseInt(OrdersTools.createOrderCode().toString());
                    int oid = goodsMapper.insertOrders(oid1,uid,id);
                    String username="321";
                    if (oid == 0){
                        System.out.print("订单创建失败");
                    }else
                    System.out.print("创建成功!");
                    //将订单放入购物车
                    int k1 = goodsMapper.insertShops(uid,oid1);
                    sqlSession.commit();
                    MybatisUtil.closeSqlSession();
                    break;
                case 4:
                    j = 4;

                    break;
                case 5:
                    j = 5;
                    break;
                case 6:
                    j = 6;
                    break;
                case 7:
                    j = 7;
                    break;
            }
        }
    }
}
